<template>
  <a-modal :title="$t('账户汇总')" :visible="visible" @cancel="handleCancel" width="50%" class="modal_title" v-drag>
    <template slot="footer">
      <a-button @click="handleCancel">{{ $t('关闭') }}</a-button>
    </template>
    <div>
      <a-row style="margin-bottom: 10px">
        <a-col :span="20">
          <div style="color: #e69167">{{ $t('说明：该汇总数据为会员账户列表查询结果的合计。') }}</div>
        </a-col>
        <a-col :span="4">
          <a-button type="primary" v-has="'BisMemberInfoController:export'" v-print="'#printme'" style="float: right">{{ $t('打印') }}</a-button>
          <a-button type="primary" v-has="'BisMemberInfoController:export'" @click="handleExportXls($t('账户汇总'))" style="float: right; margin-right: 8px">{{
            $t('导出')
          }}</a-button>
        </a-col>
      </a-row>
      <div id="printme">
        <a-table
          ref="table"
          size="middle"
          bordered
          :columns="columns"
          :dataSource="dataSource"
          :pagination="pagination"
          class="j-table-force-nowrap"
        ></a-table>
      </div>
    </div>
  </a-modal>
</template>

<script>
import { getAction, downFile } from '@/api/manage'
export default {
  name: 'MemberAccountModal',
  components: {},
  data() {
    return {
      visible: false,
      columns: [
        {
          title: this.$t('会员类型'),
          align: 'center',
          dataIndex: 'memberType_dictText',
        },
        {
          title: this.$t('会员数量'),
          align: 'center',
          dataIndex: 'count',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('代币'),
          align: 'center',
          dataIndex: 'coin',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('积分'),
          align: 'center',
          dataIndex: 'integral',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('彩票'),
          align: 'center',
          dataIndex: 'lottery',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('A票'),
          align: 'center',
          dataIndex: 'alottery',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('A币'),
          align: 'center',
          dataIndex: 'points',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
      ],
      pagination: false,
      dataSource: [],
      model: {},
      url: {
        exportXlsUrl: '/member/bisMemberInfo/totalExportXls',
      },
      exloading: false
    }
  },
  created() {},
  methods: {
    handleCancel() {
      this.visible = false
    },
    edit(record) {
      this.visible = true
      this.infoData = record
      this.model = Object.assign({}, record)
      this.model.pageNo = '1'
      this.model.pageSize = '999'
      getAction('/member/bisMemberInfo/total', this.model).then((res) => {
        this.dataSource = res.result.records
        this.dataSource.forEach((col) => {
          col.coin = (Number(col.realCoin) + Number(col.presentCoin))
        })
        if (this.dataSource.length > 0) {
          this.addSumRow(
            ['memberType_dictText', 'count', 'coin', 'integral', 'lottery', 'alottery', 'points'],
            this.dataSource
          )
        }
      })
    },
    addSumRow(myArr, dataSource) {
      let that = this,
        total = {}
      myArr.forEach(function (tcn, ind) {
        if (ind == 0) {
          total[tcn] = that.$t('合计')
        } else {
          total[tcn] = 0
        }
      })
      for (let key in total) {
        dataSource.forEach(function (ds) {
          if (!isNaN(total[key])) {
            total[key] += Number(ds[key])
          }
          if (key == 'coin') {
            ds[key] = ds[key].toFixed(2)
          }
        })
        if (key == 'coin') {
          total[key] = total[key].toFixed(2)
        }
      }
      dataSource.push(total)
    },
    handleExportXls(fileName) {
      this.exloading = true
      if (!fileName || typeof fileName != "string") {
        fileName = this.$t('导出文件')
      }
      let param = Object.assign({}, this.model);
      downFile(this.url.exportXlsUrl, param).then((data) => {
        if (!data) {
          this.$message.warning(this.$t('文件下载失败'))
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }
      }).finally(() => {
        this.exloading = false
      })
    },
  },
}
</script>
<style>
.black-row {
  margin: 0 15px;
  border: 1px solid #d9d9d9;
  padding: 10px;
  border-radius: 5px;
  font-size: 15px;
}
.top-row {
  margin: 10px;
}
</style>